﻿namespace Skymate.Commands
{
    using System.Collections.Generic;

    using Abp.Events.Bus;

    using Skymate.Lyjt.Core;

    using SuperSocket.Common;

    /// <summary>
    /// 方案调度查询应答
    /// </summary>
    public class SchedSolutionQueryResponseCommand : BaseLightCommand
    {
        public SchedSolutionQueryResponseCommand()
            : base(ProtocolConst.OptionType.QueryResponse,
                ProtocolConst.ObjectType.ProjectSchedulingPlan)
        {
        }

        public override void ExecuteCommand(LightAppSession session, SemaphoreRequestInfo requestInfo)
        {
            var message = "方案调度查询应答";
            this.ShowLog(session.RemoteEndPoint.ToString(),
                message);

            var buf = requestInfo.Body;
            var length = buf[0];

            IList<SchedSolution> timeSpans = new List<SchedSolution>();
            for (int i = 0; i < length; i++)
            {
                var buffer = buf.CloneRange(1 + SchedSolution.BufferSize * i, SchedSolution.BufferSize);
                timeSpans.Add(SchedSolution.Create(buffer));
            }

            EventBus.Default.Trigger(new SchedSolutionQueryResponseEvent(
                session.RemoteEndPoint.ToString(),
                timeSpans));
        }
    }
}